package com.interview.one.controller;

import com.interview.one.common.BusinessException;
import com.interview.one.common.CommonRes;
import com.interview.one.common.EmBusinessError;
import com.interview.one.domain.*;
import com.interview.one.model.MessageInfo;
import com.interview.one.model.UserInfo;
import com.interview.one.service.MessageService;
import com.interview.one.service.UserService;
import com.interview.one.utils.DateUtil;
import com.interview.one.utils.push.PushSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("userCenter")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MessageService messageService;
    private static final String DEFAULT_VERIFYCODE = "123456";

    @PostMapping("login")
    public CommonRes login(@Validated @RequestBody LoginReq req){
        UserInfo userInfo = this.userService.getUserByMobile(req.getMobile());
        userInfo.setPushId(req.getPushId());
        this.userService.modifyUser(userInfo);
        // sendMessage(userInfo.getId(), req.getPushId());
        return CommonRes.create(userInfo);
    }

    private void sendMessage(Integer userId, String pushId) {
        String curTime = DateUtil.format(new Date(), DateUtil.FORMAT_LONG_NEW);
        MessageInfo msg = new MessageInfo();
        msg.setMsgIcon("http://osea2fxp7.bkt.clouddn.com/108x108.png");
        msg.setMsgTitle("登录成功");
        msg.setMsgContent("恭喜您登录成功");
        msg.setMsgTime(curTime);
        msg.setUserId(userId);
        this.messageService.addMessage(msg);

        PushSender.sendLoginEvent(pushId);
    }

    @PostMapping("/forgetPwd")
    public CommonRes forgetPwd(@Validated @RequestBody ForgetPwdReq req) throws BusinessException {
        UserInfo userInfo = this.userService.getUserByMobile(req.getMobile());
        if(!DEFAULT_VERIFYCODE.equals(req.getVerifyCode())){
            throw new BusinessException(EmBusinessError.VERY_CODE_FAIL);
        }
        return CommonRes.create(userInfo);
    }

    @PostMapping("/resetPwd")
    public CommonRes resetPwd(@Validated @RequestBody ModifyPwdReq req) throws BusinessException {
        String mobile = req.getMobile();
        String pwd = req.getPwd();
        UserInfo userInfo = this.userService.getUserByMobile(mobile);
        userInfo.setUserPwd(pwd);
        int result = this.userService.modifyUser(userInfo);
        if (result > 0){
            return CommonRes.create("密码修改成功");
        }else{
            throw new BusinessException(EmBusinessError.RESET_PWD_FAIL);
        }
    }

    @PostMapping("/register")
    public CommonRes register(@Validated @RequestBody RegisterReq req){
        UserInfo userInfo = this.userService.getUserByMobile(req.getMobile());
        userInfo = new UserInfo();
        userInfo.setUserMobile(req.getMobile());
        userInfo.setUserName(req.getMobile());
        userInfo.setUserPwd(req.getPwd());
        this.userService.addUser(userInfo);
        return CommonRes.create(userInfo);
    }

    @PostMapping("/editUser")
    public CommonRes editUser(@Validated @RequestBody EditUserReq req, @RequestHeader String token) {
        UserInfo userInfo = this.userService.getUserById(Integer.parseInt(token));

        userInfo.setId(Integer.valueOf(token));
        userInfo.setUserName(req.getUserName());
        userInfo.setUserIcon(req.getUserIcon());
        userInfo.setUserGender(req.getGender());
        userInfo.setUserSign(req.getSign());
        userInfo.setUserPwd(null);

        this.userService.modifyUser(userInfo);
        return CommonRes.create(userInfo);
    }
}
